package com.lijiajian.controller;

import com.lijiajian.Service.RoleService;
import com.lijiajian.annoation.RequiredPermission;
import com.lijiajian.base.BaseController;
import com.lijiajian.base.ResultInfo;
import com.lijiajian.pojo.Role;
import com.lijiajian.query.RoleQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController extends BaseController {
    @Resource
    private RoleService roleService;

    /**
     * 查出所有的角色名
     * @param id
     * @return
     */
    @GetMapping("queryAllRoles")
    @ResponseBody
    public List<Map<String,Object>> getAllRoles(Integer id){
        return roleService.getAllRoles(id);
    }

    /**
     * 前往角色列表
     * @return
     */
    @RequiredPermission(code="6020")
    @GetMapping("index")
    public String toIndex(){
        return "role/role";
    }

    /**
     * 得到角色列表
     * @param roleQuery
     * @return
     */
    @RequiredPermission(code="602002")
    @GetMapping("list")
    @ResponseBody
    public Map<String,Object> getRoleList(RoleQuery roleQuery){
        return roleService.queryByParamsForTable(roleQuery);
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @RequiredPermission(code="602001")
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addRole(Role role){
        roleService.addRole(role);
        return success("添加成功");
    }
    /**
     * 前往添加或修改页面
     */
    @GetMapping("toAddAndUpdatePage")
    public String toAddAndUpdatePage(Integer roleId, HttpServletRequest request){
        if(roleId!=null){
            //前往添加页面
            request.setAttribute("role",roleService.selectByPrimaryKey(roleId));
        }
        return "role/addAndUpdateRole";
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @RequiredPermission(code="602003")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateRole(Role role){
        roleService.updateRole(role);
        return success("修改成功");
    }
    @RequiredPermission(code="602004")
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteRoles(Integer[] roleIds){
        roleService.deleteRoles(roleIds);
        return success("删除成功");
    }

    /**
     * 授权
     * @param roleId
     * @param mIds
     * @return
     */
    @RequiredPermission(code="602005")
    @PostMapping("addGrant")
    @ResponseBody
    public ResultInfo addGrant(Integer roleId,Integer[] mIds){
        roleService.addGrant(roleId,mIds);
        return success("授权成功");
    }
}
